param(
    [Parameter(Mandatory = $true)]
    [String]$CloudCompareInstallFolder,

    [Parameter()]
    [ValidateSet("En", "Fr")]
    [String]$Localization = "En",

    [Parameter()]
    [ValidateSet("ON", "OFF")]
    [String]$PLUGIN_STANDARD_QM3C2 = "OFF"
)

$IsFolder = Test-Path -Path $CloudCompareInstallFolder -PathType Container
if ($IsFolder -eq $False) {
    throw '$CloudCompareInstallFolder does no point to a Folder'
}

# we need to find two DLLs
# A python3.dll
# A python3<minor version>.dll
$PythonDlls = Get-ChildItem -Path $CloudCompareInstallFolder -Filter "python3*.dll"
Write-Host "Python DLL suffix: $PythonDlls"
if ($PythonDlls.count -ne 2) {
    throw "Need 2 Python3 DLLs, found ", $PythonDlls.count
} else {
    if($PythonDlls[0].Name -ne "python3.dll") {
        throw "python3.dll not found"
    }
    $re = [Regex]::new("python3(?<Suffix>[0-9]+).dll")
    $Python3DllSuffix = $re.Match($PythonDlls[1].Name).Groups['Suffix'].Value
}

$LocalizationFile = if ($Localization -eq "Fr") { "french.wxl" } else { "english.wxl" }
$LocalizationSwitch = if ($Localization -eq "Fr") { "fr-fr" } else { "en-us" }
$LocalizationName = if ($Localization -eq "Fr") { "French" } else { "English" }

$PythonEnvPrefix = Join-Path -Resolve -Path $CloudCompareInstallFolder -ChildPath "plugins" -AdditionalChildPath "Python"
$EnvTypeName = if (Test-Path -Path (Join-Path -Path $PythonEnvPrefix -ChildPath "conda-meta")) { "Conda" } else { "Venv" }

Write-Host ""
Write-Host "Python DLL suffix (minor version number): $Python3DllSuffix"
Write-Host "Localization name: $LocalizationName"
Write-Host "Environment type : $EnvTypeName"
Write-Host "Plugin QM3C2     : $PLUGIN_STANDARD_QM3C2"
Write-Host ""

# # Scans the Python environment dir and
# # generates a Wix source file
&heat `
    dir "$CloudCompareInstallFolder\plugins\Python" `
    -scom `
    -frag `
    -srd `
    -sreg `
    -gg `
    -cg PythonEnvironment `
    -dr PythonEnvironmentDir `
    -o PythonEnvironment.wxs

# Compiles the Wix Source files
# with the given variables
&candle `
    -arch x64 `
    -dSourceInstallPath="$CloudCompareInstallFolder" `
    -dPythonSuffix="$Python3DllSuffix" `
    -dPLUGIN_STANDARD_QM3C2="$PLUGIN_STANDARD_QM3C2" `
    .\Installer.wxs `
    .\PythonEnvironment.wxs `

$OutputInstallerName = "CloudCompare-PythonRuntime-Setup-$LocalizationName-Python$Python3DllSuffix-$EnvTypeName.msi"

# Link the compiled files together
&light `
    -ext WixUIExtension `
    .\Installer.wixobj `
    .\PythonEnvironment.wixobj `
    -b "$CloudCompareInstallFolder\plugins\Python" `
    -b "$CloudCompareInstallFolder\resources" `
    -b "$(Get-Location)" `
    -dWixUILicenseRtf="GPLv3_en.rtf" `
    -dWixUIBannerBmp="TopBanner.bmp" `
    -dWixUIDialogBmp="UiBanner.bmp" `
    -cultures:$LocalizationSwitch `
    -loc $LocalizationFile `
    -o $OutputInstallerName

Write-Host "Installer created at " (Join-Path $pwd $OutputInstallerName)

# The heat command is taken from https://stackoverflow.com/questions/26550763/wix-how-to-copy-a-directory-to-install-folder
# Basically:
# it 'harvest' the "$CloudCompareInstallFolder\plugins\Python" directory
#   -scom -sfrag -srd -sreg = Suppress COM elements, fragments, root directory as element, registry harvesting (these options will create a grouping that most applications can use)
#   -gg = generate GUID's now (this will put the GUID's into your output file, rather than using a wildcard "*". The advantage here is you can use non-standard TARGETDIR, which would not qualify for autogenerated GUID's)
#   -cg PythonEnvironment : The ID we reference in the <Fearure> of the Installer.wxs
#    -dr PythonEnvironmentDir : ID of the directory (in Installer.wxs) were harvest files will be installed


# https://www.firegiant.com/wix/tutorial/user-interface/ui-wizardry/
